package com.yugao.fintech.draper.message.queue.support;

import com.yugao.fintech.draper.message.queue.message.AbstractMessage;

import java.util.ArrayList;
import java.util.List;

public abstract class MessageQueueInterceptorAccessor<T extends MessageQueueInterceptor> {
    /**
     * 消息处理拦截器
     */
    private final List<T> interceptors = new ArrayList<>();

    /**
     * 消息发送前处理
     */
    protected void sendMessageBefore(AbstractMessage message) {
        interceptors.forEach(interceptor -> interceptor.sendMessageBefore(message));
    }

    /**
     * 消息发送后处理
     */
    protected void sendMessageAfter(AbstractMessage message) {
        for (int i = interceptors.size() - 1; i >= 0; i--) {
            interceptors.get(i).sendMessageAfter(message);
        }
    }

    /**
     * 获取拦截器
     */
    public List<T> getInterceptors() {
        return interceptors;
    }

    /**
     * 添加拦截器
     */
    public void addInterceptor(List<T> messageQueueInterceptors) {
        interceptors.addAll(messageQueueInterceptors);
    }
}
